<?php

class Banner extends CI_Controller {
    
    public $_updir = 'public/template/backend/images/banner/';
    private $_updat;
    private $_auth;
    public function __construct() {
        parent::__construct();
        $this->_auth = $this->myclass->checkuser();
        if($this->_auth == FALSE) redirect ("backend/home");
    }
    
    public function index(){
        $data['data']['parcat']= $this->mbanner->fetchAll();
        $data['title'] = "Bảng banner";
        $data['temp'] = "backend/banner/home";
        $this->load->view("backend/layout/index",$data);
    }
    
    public function add() {
        if ($this->input->post("add")) {           
            $this->form_validation->set_error_delimiters('<li>', '</li>');
            $config['upload_path'] = $this->_updir;
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '2000';
            $config['overwrite'] = TRUE;
            $this->load->library('upload', $config);

            if ($this->upload->do_upload('img')) {
                $this->_updat = $this->upload->data();
                $post['img'] = $this->_updir . $this->_updat['file_name'];               
                $this->mbanner->insert($post);
                redirect("backend/banner");
            } else {
                $data['data'] = array('error' => $this->upload->display_errors());
            }
        }
        $data['title'] = "Thêm banner";
        $data['temp'] = "backend/banner/form";
        $this->load->view("backend/layout/index", $data);
    }
    
    public function update($id) {
        $data['data']['img'] = $this->mbanner->fetchOne($id);
        if ($this->input->post("add")) {
            $config['upload_path'] = $this->_updir;
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '2000';
            $config['overwrite'] = TRUE;
            $this->load->library('upload', $config);
            if ($this->upload->do_upload('img')) {
                $this->_updat = $this->upload->data();
                $post['img'] = $this->_updir . $this->_updat['file_name'];
            } else {
                $post['img'] = $data['data']['img']->img;
            }
            $this->mbanner->update($id, $post);
            redirect("backend/banner");
        }

        $data['title'] = "Sửa banner";
        $data['temp'] = "backend/banner/form";
        $this->load->view("backend/layout/index", $data);
    }
    
    public function delete($id){
        $this->mbanner->delete($id);
        redirect("backend/banner");
    }
    
    public function deleteall(){
        $this->mbanner->deleteAll();
        redirect("backend/banner");
    }

}
